//
//  main.cpp
//  test
//
//  Created by fox on 12-11-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//




#include "netServer.h"
#include "dbDataBase.h"


#include "NpcManager.h"
#include "ItemManager.h"
#include "BattleManager.h"
#include "MapManager.h"
#include "GameString.h"
#include "GameConfig.h"
#include "ExpConfig.h"
#include "CreatureManager.h"
#include "NetMsgHandler.h"

#include "iconv.h"


fvoid RunServerExit();
extern fvoid InitException();


netServer* gNetServer;
dbIDatabase* gDBServer;

fbool gIsRun = F_TRUE;
fbool gIsExit = F_TRUE;


fvoid   dbexit( dbResult* result )
{
    
}

fvoid   RunServerExit()
{
//    for ( int i = 0 ; i < 25550 ; i++ )
//    {
//        // save all data,,
//        gDB->ExecuteAsyncSQL( "SELECT * FROM new_table;" , 0 , &dbexit );
//    }
    
    while( gIsExit && gDBServer )
    {
        fuint32 size = gDBServer->GetRequestSize();
        
        FLOG4( "db request %d." , size );
        
        if ( !size )
        {
            gIsExit = F_FALSE;
        }
    }
}




fvoid	LoadData()
{
	NpcManager::Instance()->Init();
    gNpcManager->LoadTemplate( "npcTemplate.xml" );
    gNpcManager->Load( "npc/100/" );

    GameConfig::Instance()->init( "gameConfig.xml" );
	
	ItemManager::Instance()->Load( "item.xml" );
	ExpConfig::Instance()->Load( "exp.xml" );

	gCreatureManager->LoadEnemyBase( "EnemyBase.xml" );
	gCreatureManager->LoadEnemy( "Enemy.xml" );
	gCreatureManager->LoadEnemyGroup( "Group.xml" );
	//gCreatureManager->LoadPetPhoto( "petPhoto.xml" );

    MapManager::Instance()->LoadMap();
	MapManager::Instance()->LoadMapWarp( "mapWarp.xml" );
	MapManager::Instance()->LoadEnemyCount( "encount.xml" );

    
    FLOG4( "map Inited." );
    
    gBattleManager->Init();
    FLOG4( "battle Inited." );
}


fvoid   ServerInit()
{
    GameString::Instance();
	CreatureManager::Instance();
	BattleManager::Instance();
   
    
	InitNetMsg();
    
	gCreatureManager->Init();
	
	FLOG4( "net Init." );
	FLOG4( "CreatureManager Init." );
	FLOG4( "Loading data...");
	
	LoadData();
    
    
    gDBServer = DatabaseCreate("127.0.0.1" , "root" , "" , "sagame" );
    
    gNetServer = new netServer();
    gNetServer->SetAddress( "127.0.0.1" , 5777 );

    
    gNetServer->SetHandler( OnNetMsgEnter , OnNetMsgExit , OnNetMsg );
    gNetServer->Start();
    
    
    FLOG4( "server started." );
    
    //InitNetMsg();
    InitException();

    //((char *)NULL)[1] = 0;
    
    
}


fvoid   GameRun( freal32 delay )
{
    
    // logic run here ,
    
    gMapManager->Update( delay );
    
}


fvoid   ServerRun()
{
    timer t;
    freal32 now = 0;
    freal32 last = 0;
    
    srand( t.elapsed() );
	
    fint32 count = 0;
    
    
    while ( gIsRun )
    {
        // test,,
        
        if ( count > 10000 )
        {
#ifdef WIN32
            Sleep( 1 );
#else
            usleep( 30 );
#endif
            count = 0;
        }
        
        count++;
        
//        dbtest test;
//        char* pc = gDB->ConvertBinaryToString( (char*)&test , sizeof( dbtest ) );
//        
//        FLOG( LOGLEVEL4 , "%s" , pc );
//        
//        char buffer[ 2048 ] = {};
//        SPRINTF( buffer , "CALL testUpdate('%s')" , pc );
//        gDB->ExecuteAsyncSQL( buffer , 0 , &DBTest2 );
//        gDB->ExecuteAsyncSQL( "CALL testSelect()" , 0 , &DBTest1 );
        
        
        dbResult* pRessult = F_NULL;
		while ( ( pRessult = gDBServer->GetAsyncResult() ) != F_NULL)
		{
			pRessult->callback( pRessult );
			pRessult->Release();
		}
        
        gNetServer->Update();
        
        now = t.elapsed();
        
        GameRun( now - last );
        
        last = now;
        
    }
}


int main(int argc, const char * argv[])
{
    ServerInit();
    
    ServerRun();
    
    RunServerExit();
    
    return 0;
}

